//
//  DKEQViewController.m
//  111
//
//  Created by 丁侃 on 16/4/19.
//  Copyright © 2016年 丁侃. All rights reserved.
//

#import "DKEQViewController.h"
#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenheight [UIScreen mainScreen].bounds.size.height

static CGFloat const topY = 64;
static CGFloat const topH = 44;


@interface DKEQViewController ()<UIScrollViewDelegate>


@property (nonatomic, weak) UIScrollView *topBar;


//存放之前的按钮
@property (nonatomic, strong) UIButton *preBtn;

@property (nonatomic, strong) UIColor *titleColor;

@end

@implementation DKEQViewController

-(UIColor *)titleColor
{
    if (!_titleColor) {
        _titleColor = [UIColor redColor];
    }
    return _titleColor;
}

-(UIScrollView *)bottomView
{
    if (!_bottomView) {
        
        UIScrollView *bottomView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.topBar.frame), screenWidth, screenheight - CGRectGetMaxY(self.topBar.frame))];
        
        bottomView.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:bottomView];
        
        _bottomView = bottomView;
        
        //监听scrollView的滚动
        _bottomView.delegate = self;
    }
    return _bottomView;
}

-(UIScrollView *)topBar
{
    if (!_topBar) {
        
        //如果有导航条,就空出距离
        CGFloat topBarY = self.navigationController? topY:0;
        
        UIScrollView *topBar = [[UIScrollView alloc]initWithFrame:CGRectMake(0, topBarY, screenWidth, topH)];
        
        topBar.showsHorizontalScrollIndicator = NO;
        topBar.showsVerticalScrollIndicator = NO;
        
        [self.view addSubview:topBar];
        
        _topBar = topBar;
    }
    return _topBar;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加顶部滚动条
    [self topBar];
    
    //添加底部显示的滚动条
    [self bottomView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //代码的分装,
    if (self.automaticallyAdjustsScrollViewInsets) {
        
        //当时视图显示的时候,设置不让系统自动添加额外的区域
        self.automaticallyAdjustsScrollViewInsets = NO;
        
        _bottomView.contentSize = CGSizeMake(self.childViewControllers.count * screenWidth, 0);
        
        _bottomView.showsVerticalScrollIndicator = NO;
        _bottomView.showsHorizontalScrollIndicator = NO;
        
        _bottomView.pagingEnabled = YES;
        
        //默认颜色为红色
        self.titleColorR = 1;
        self.titleColor = [UIColor colorWithRed:self.titleColorR green:self.titleColorG blue:self.titleColorB alpha:1];
        [self setupTopBtn];
    }
    
}
#pragma  mark -------------------------------
#pragma  mark -UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获取索引
    NSInteger index = scrollView.contentOffset.x / screenWidth;
    
    //获取比例
    CGFloat scale = scrollView.contentOffset.x / screenWidth - index;
    //获取所在view的索引
    NSInteger currentIndex = index;
    //获取将要显示的索引
    NSInteger futureIndex = index + 1;
    
    //当前的按钮缩放比例
    CGFloat currentScale = 1 - scale;
    //将要显示的按钮的缩放比例
    CGFloat futureScale = scale;
    
    //最左边
    if (futureScale < 0) {
        futureScale = 0;
    }
    
    //最右边
    if (futureIndex > self.childViewControllers.count - 1) {
        futureIndex = self.childViewControllers.count - 1;
        futureScale = 1;
    }
    //获取当前所在的按钮
    UIButton *currentBtn = self.topBar.subviews[currentIndex];
    //获取将要显示的按钮
    UIButton *futureBtn = self.topBar.subviews[futureIndex];
    
    [currentBtn setTitleColor:[self setColorWithScale:currentScale] forState:UIControlStateNormal];
    [futureBtn setTitleColor:[self setColorWithScale:futureScale] forState:UIControlStateNormal];
    
    currentBtn.transform = CGAffineTransformMakeScale( 1 + self.titleScale * currentScale, 1 + self.titleScale * currentScale);
    futureBtn.transform = CGAffineTransformMakeScale(1 + self.titleScale * futureScale, 1 + self.titleScale * futureScale);
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    //动滚动动画结束,回复其他按钮的颜色和缩放大小
    for (UIButton *otherBtn  in self.topBar.subviews) {
        if (otherBtn != self.preBtn) {
            [otherBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            otherBtn.transform = CGAffineTransformMakeScale(1, 1);
        }
    }
}


//设置颜色
-(UIColor *)setColorWithScale:(CGFloat )scale
{
    CGFloat red = self.titleColorR * scale;
    CGFloat green = self.titleColorG * scale;
    CGFloat blue = self.titleColorB * scale;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

//设置偏移
-(void)setCurrentTitleBtn:(UIButton *)btn
{
    //左
    CGFloat space = btn.center.x - 0.5 * screenWidth;
    if (space < 0) {
        space = 0;
    }
    
    //右
    CGFloat maxContentSpace = self.topBar.contentSize.width - screenWidth;
    
    if (space > maxContentSpace) {
        space = maxContentSpace;
    }
    
    [self.topBar setContentOffset:CGPointMake(space, 0) animated:YES];
}

//当滚动结束会调用此方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / screenWidth;
    
    UIButton *btn = self.topBar.subviews[index];
    
    //添加view
    [self click:btn];
    
    //设置偏移
    [self setCurrentTitleBtn:btn];
    
}

//添加顶部的按钮
-(void)setupTopBtn
{
    NSInteger count = self.childViewControllers.count;
    
    CGFloat btnH = _topBar.bounds.size.height;
    CGFloat btnW = self.topBar.bounds.size.width / self.childViewControllers.count;
    for (NSInteger i = 0; i < count; i ++) {
        
        UIViewController *vc = self.childViewControllers[i];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //设置按钮标题
        [btn setTitle:vc.title forState:UIControlStateNormal];
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        btn.frame = CGRectMake(btnW * i, 0, btnW, btnH);
        
        [_topBar addSubview:btn];
        
        btn.tag = i;
        
        if (i == 0) {
            [self click:btn];
        }
        
        //监听按钮的点击
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    //设置滚动区域
    _topBar.contentSize = CGSizeMake(count * btnW, 0);
}

//点击按钮,添加view
-(void)click:(UIButton *) btn
{
    
    //添加控制器的view
    [self addOneChildVcView:btn];
    
    //设置偏移
    CGFloat bottomW = self.bottomView.bounds.size.width;
    [self.bottomView setContentOffset:CGPointMake(btn.tag * bottomW, 0) animated:YES];
    
    self.preBtn = btn;
}


//添加一个控制器
-(void)addOneChildVcView:(UIButton *)btn
{
    UIViewController *vc = self.childViewControllers[btn.tag];
    CGFloat bottomW = self.bottomView.bounds.size.width;
    CGFloat bottomH = self.bottomView.bounds.size.height;
    
    vc.view.frame = CGRectMake(bottomW * btn.tag, 0, bottomW, bottomH);
    
    //如果子控制器的view已经添加过了,就不需要再次添加
    if (!vc.view.superview){
        
        [self.bottomView addSubview:vc.view];
    }
}
@end
